



function addMaterialToStockOrder(){

	//得到要添加记录的表格---tbody
	var mbody = document.getElementById("mbody");
	//向表格中增加一行记录
	var tr = mbody.insertRow(mbody.rows.length) ;
	//表示行的下标
	var rowIndex = mbody.rows.length-1 ;
	
	//增加一个单元格
	
	var cell = tr.insertCell(0); 
	
	cell.innerHTML = "<td><input type='hidden' name='stockorderdetail' id='stockorderdetail"+rowIndex+"'><input type='checkbox' name='checkbox'></td>" ;
	//物料代码
	cell = tr.insertCell(1);
	
	cell.innerHTML = "<td><input type='hidden' name='materialId' id='materialId"+rowIndex+"'> <input name='' id='materialCode"+rowIndex +"'type='text' size='10' ondblclick='openWinMaterial("+rowIndex+",800,600,1)'></td>" ;
	//物料名称
	cell = tr.insertCell(2) ;
	
	cell.innerHTML = "<td><input type='text' size='10'  readonly='true'  id='materialName"+rowIndex+"'></td>" ;
	//规格型号
	cell = tr.insertCell(3);
	
	cell.innerHTML = "<td><input type='text' size='10'  readonly='true' id='spec"+rowIndex+"'></td>" ;
	//基本单位名称
	cell = tr.insertCell(4);
	
	cell.innerHTML = "<td><input type='text' size='10'  readonly='true' id='basicMeasureUnitName"+rowIndex+"'></td>" ;
	//基本单位数量
	cell = tr.insertCell(5) ;
	
	cell.innerHTML = "<td><input type='text' size='10'  readonly='true' name='' id='basicCost"+rowIndex+"'></td>" ;
	//单位
	cell = tr.insertCell(6);
	
	cell.innerHTML = "<td><input type='hidden' name='measureUnitId' id='measureUnitId"+rowIndex+"'><input type='text' size='10' name='' id='measureUnitName"+rowIndex+"' ondblclick='openWinMeasureUnit(" + rowIndex + ",800,600,0)'></td>";

	//数量
	cell = tr.insertCell(7);
	
	cell.innerHTML = "<td><input type='text' size='10' name='cost' id='cost"+rowIndex+"' onblur='amount("+rowIndex+")'></td>" ;
	
	//单价
	cell = tr.insertCell(8);
	
	cell.innerHTML = "<td><input type='text' size='10' name='unitPrice' id='unitPrice"+rowIndex+"' onblur='amount("+rowIndex+")'></td>" ;
	
	//含税单价
	cell = tr.insertCell(9);
	
	cell.innerHTML = "<td><input type='text' size='10' name='totalUnitPrice' id='totalUnitPrice"+rowIndex+"'></td>" ;
	
	//金额
	cell = tr.insertCell(10);
	
	cell.innerHTML = "<td><input type='text' size='10' name='money' id='money"+rowIndex+"'></td>" ;
	
	//折扣率
	cell = tr.insertCell(11);
	
	cell.innerHTML = "<td><input type='text' size='10' name='discount' id='discount"+rowIndex+"' onblur='amount("+rowIndex+")'></td>" ;
	
	//实际含税单价
	cell = tr.insertCell(12);
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name='realTotalUnitPrice' id='realTotalUnitPrice"+rowIndex+"'></td>" ;

	//折扣额
	cell = tr.insertCell(13);
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name='discountPrice' id='discountPrice"+rowIndex+"'></td>" ;
	
	//税率
	cell = tr.insertCell(14);
	
	cell.innerHTML = "<td><input type='text' size='10' name='dutyFrank' id='dutyFrank"+rowIndex+"' onblur='amount("+rowIndex+")'></td>" ;
	
	//税额
	cell = tr.insertCell(15);
	
	cell.innerHTML = "<td><input type='text' size='10' readonly='true' name='dutyPrice' id='dutyPrice"+rowIndex+"'></td>" ;
	
	//价格合计
	cell = tr.insertCell(16);
	
	cell.innerHTML = "<td><input type='text' size='10' name='totalMoney' id='totalMoney"+rowIndex+"'></td>" ;
	
	//交货日期
	cell = tr.insertCell(17);
	
	cell.innerHTML = "<td><input type='text' size='10' name='deliveryDate' id='deliveryDate"+rowIndex+"' class='hand' readonly='true' onclick='time.setHook1(this)'></td>" ;
	
	//备注
	cell = tr.insertCell(18);
	
	cell.innerHTML = "<td><input type='text' size='10' name='remark'></td>" ;
	
}



function openWinMaterial(index, width, height, scroll) {
	//alert(this.parentElement.parentElement.rowIndex);
	var truelink = "selectMaterial.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}

function childToParentForMaterial(index,id, childId, name, childName, code, childCode, materialAttribute, childMaterialAttribute, spec, childSpec,storageName,childStorageName,storageId,childStorageId,measureUnitName,childMeasureUnitName,measureUnitId,childMeasureUnitId,basicMeasureUnitName,childBasicMeasureUnitName) {
	var t = window.opener.document.getElementById("mbody");	
	//物料id
	var parentId = window.opener.document.getElementById(id + index);
	//物料name
	var parentName = window.opener.document.getElementById(name+ index);
	//物料code
	var parentCode = window.opener.document.getElementById(code + index);
	//var parentMaterialAttribute = window.opener.document.getElementById(materialAttribute+ index);
	var parentSpec = window.opener.document.getElementById(spec+ index);
	//var parentStorageName =  window.opener.document.getElementById(storageName+ index);
	//计量单位
	var parentMeasureUnitName =  window.opener.document.getElementById(measureUnitName+ index);
	var parentMeasureUnitId = window.opener.document.getElementById('measureUnitId'+ index);
	//基本计量单位
	var parentBasicMeasureUnitName =  window.opener.document.getElementById(basicMeasureUnitName+ index);
	//数量
	var parentCost =  window.opener.document.getElementById('cost'+ index);
	//基本单位数量
	var parentBasicCost =  window.opener.document.getElementById('basicCost'+ index);
	
	//单价
	var unitPrice = window.opener.document.getElementById('unitPrice'+index);
	//含税单价
	var totalUnitPrice = window.opener.document.getElementById('totalUnitPrice'+index);
	//金额
	var money = window.opener.document.getElementById('money'+index);
	//折扣率
	var discount = window.opener.document.getElementById('discount'+index);
	//税率
	var dutyFrank = window.opener.document.getElementById('dutyFrank'+index);
	
	//实际含税单价	
	var realTotalUnitPrice = window.opener.document.getElementById('realTotalUnitPrice'+index);
	//折扣额
	var discountPrice = window.opener.document.getElementById('discountPrice'+index);
	
	//税额
	var dutyPrice = window.opener.document.getElementById('dutyPrice'+index);
	
	//价格合计
	var totalMoney = window.opener.document.getElementById('totalMoney'+index);
	
	//到货时间处理
	var date  = new Date();
	var year = date.getYear();
	var month = date.getMonth()+1;
	var day = date.getDate();
	var vdate = year+'-'+month+'-'+day;
	var deliveryDate = window.opener.document.getElementById('deliveryDate'+index);
	deliveryDate.value = vdate;
	
	//var parentUsedState =  window.opener.document.getElementById('usedState'+ index);
	//var parentStorageId =  window.opener.document.getElementById('storageId'+ index);
	
	parentId.value = childId;
	parentName.value = childName;
	parentCode.value = childCode;
	//parentMaterialAttribute.value = childMaterialAttribute;
	parentSpec.value = childSpec;
	//parentStorageName.value = childStorageName;
	parentMeasureUnitName.value = childMeasureUnitName;
	
	parentMeasureUnitId.value = childMeasureUnitId;
	parentBasicMeasureUnitName.value = childBasicMeasureUnitName;
	
	//数量
	parentCost.value = '0.00';
	//基本单位数量
	parentBasicCost.value = '1.00';
	//单价
	unitPrice.value='0.00';
	
	//含税单价
	totalUnitPrice.value='0.00' ;
	//金额
	money.value = '0.00'; 
	//折扣率
	discount.value  = '0.00' ;
	//实际含税单价
	realTotalUnitPrice.value='0.00' ;
	
	//折扣额
	discountPrice.value = '0.00'; 
	
	//税率
	dutyFrank.value =17 ;
	//税额
	dutyPrice.value='0.00';
	//合计金额
	totalMoney.value='0.00';
	//parentUsedState.value = '使用';
	//parentStorageId.value=childStorageId;	

	window.close();
}

function openWinMeasureUnit(index, width, height, scroll){
	var truelink = "selectMeasure.action?rowIndex="+index;
	openWin(truelink, null, width, height, scroll);
}

//明细--数量/单价事件触发函数
function amount(rowIndex){
	//物料数量
	var cost = document.getElementById('cost'+rowIndex);
	var vcost = parseFloat(cost.value);
	
	//物料单价
	var unitPrice = document.getElementById('unitPrice'+rowIndex);
	var vunitPrice = parseFloat(unitPrice.value);
	
	//金额
	var money = document.getElementById('money'+rowIndex);
	var vmoney = parseFloat(money.value);
	//含税单价
	var totalUnitPrice = document.getElementById('totalUnitPrice'+rowIndex);
	var vtotalUnitPrice = parseFloat(totalUnitPrice.value);
	//折扣率
	var discount = document.getElementById('discount'+rowIndex);
	var vdiscount  = parseFloat(discount.value);
	//折扣额
	var discountPrice = document.getElementById('discountPrice'+rowIndex);
	var vdiscountPrice = parseFloat(discountPrice.value);
	//税额
	var dutyPrice = document.getElementById('dutyPrice'+rowIndex);
	//实际含税单价
	var realTotalUnitPrice = document.getElementById('realTotalUnitPrice'+rowIndex);
	var vrealTotalUnitPrice = parseFloat(realTotalUnitPrice.value);
	//税率
	var dutyFrank = document.getElementById('dutyFrank'+rowIndex);
	var vdutyFrank = parseFloat(dutyFrank.value);
	//价税合计
	var totalMoney = document.getElementById('totalMoney'+rowIndex);
	var vtotalMoney = parseFloat(totalMoney.value);
	//价格不为0
	if(vunitPrice>0.0){
		totalUnitPrice.value = vunitPrice*(1+dutyFrank.value/100);
	}
	//数量不为0
	if(vcost>0.0){
		getBasicCostInfo(rowIndex);
	}
	//数量价格都不为0
	if(vcost>0.0 || vunitPrice>0.0){
				
		money.value = vcost*vunitPrice ;
		totalUnitPrice.value = vunitPrice*(1+dutyFrank.value/100);
		totalMoney.value = totalUnitPrice.value*vcost;
		
		//有折扣的情况
		if(vdiscount>0.0){
		
			//实际含税单价
			realTotalUnitPrice.value= vtotalUnitPrice*(1-0.01*vdiscount) ;
			//价税合计
			totalMoney.value = realTotalUnitPrice.value*vcost ;
			//折扣额
			discountPrice.value = vcost*totalUnitPrice.value*vdiscount/100;
			
			//税额
			dutyPrice.value = vcost*vunitPrice*(1-0.01*vdiscount)*dutyFrank.value/100;						
		}
		
	}
}




